/*
 * UserTypeForm.java
 *
 * Created on April 20, 2008, 1:20 AM
 */

package com.floreantpos.ui.forms;

import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.gjt.sp.jedit.gui.CheckBoxListModel;
import org.gjt.sp.jedit.gui.JCheckBoxList;
import org.gjt.sp.jedit.gui.JCheckBoxList.Entry;

import com.floreantpos.model.UserPermission;
import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.UserTypeDAO;
import com.floreantpos.model.util.IllegalModelStateException;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.POSMessageDialog;

/**
 *
 * @author  rodaya
 */
public class UserTypeForm extends BeanEditor {
    private UserType userType;
    
    /** Creates new form UserTypeForm */
    public UserTypeForm() {
        this(null);
    }

    public UserTypeForm(UserType type) {
    	this.userType = type;
    	
    	initComponents();
    	
    	listPermissions.setModel(UserPermission.permissions);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfTypeName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listPermissions = new JCheckBoxList();

        jLabel1.setText(com.floreantpos.POSConstants.TYPE_NAME + ":");

        jLabel2.setText(com.floreantpos.POSConstants.PERMISSIONS + ":");

        jScrollPane1.setViewportView(listPermissions);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfTypeName, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfTypeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private JCheckBoxList listPermissions;
    private javax.swing.JTextField tfTypeName;
    // End of variables declaration//GEN-END:variables
	@Override
	public void dispose() {
	}

	@Override
	public String getDisplayText() {
		return null;
	}

	@Override
	public boolean save() {
		try {
			if(!updateModel()) {
				return false;
			}
		} catch (IllegalModelStateException e) {
			POSMessageDialog.showError(e.getMessage());
			return false;
		}
		
		UserTypeDAO dao = new UserTypeDAO();
		dao.saveOrUpdate(userType);
		
		return true;
	}

	@Override
	protected boolean updateModel() throws IllegalModelStateException {
		if(userType == null) {
			userType = new UserType();
		}
		
		String name = tfTypeName.getText();
		if(StringUtils.isEmpty(name)) {
			throw new IllegalModelStateException(com.floreantpos.POSConstants.TYPE_NAME_CANNOT_BE_EMPTY);
		}
		
		userType.setName(name);
		userType.clearPermissions();
		
		Object[] checkedValues = (Object[]) listPermissions.getCheckedValues();
		for (int i = 0; i < checkedValues.length; i++) {
			userType.addTopermissions((UserPermission) checkedValues[i]);
		}
		
		setBean(userType);
		return true;
	}

	@Override
	protected void updateView() {
		if(userType == null) {
			listPermissions.clearSelection();
			return;
		}
		
		tfTypeName.setText(userType.getName());
		
		Set<UserPermission> permissions = userType.getPermissions();
		if(permissions == null) {
			listPermissions.clearSelection();
			return;
		}
		
		CheckBoxListModel model = (CheckBoxListModel) listPermissions.getModel();
		for (UserPermission permission : permissions) {
			for (int i = 0; i < model.getItems().size(); i++) {
				Entry entry = (Entry) model.getItems().elementAt(i);
				if(entry.getValue().equals(permission)) {
					entry.setChecked(true);
				}
			}
		}
		model.fireTableRowsUpdated(0, model.getRowCount());
	}

	public UserType getUserType() {
		return userType;
	}

	public void setUserType(UserType userType) {
		this.userType = userType;
		setBean(userType);
		
		updateView();
	}
    
}
